package com.foucus.myfouces.MVP.View.views.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.foucus.myfouces.Api.GankService;
import com.foucus.myfouces.Base.BaseActivity;
import com.foucus.myfouces.Base.BaseReq;
import com.foucus.myfouces.Base.Setting;
import com.foucus.myfouces.MVP.View.views.Fragment.FollowFg;
import com.foucus.myfouces.MVP.View.views.Fragment.HomePageFg;
import com.foucus.myfouces.MVP.View.views.Fragment.MyFg;
import com.foucus.myfouces.MVP.View.views.Fragment.SkillFg;
import com.foucus.myfouces.MVP.Model.ArgumentResp;
import com.foucus.myfouces.R;
import com.foucus.myfouces.Resource.HttpUtil.ServiceFactory;
import com.foucus.myfouces.Resource.Util.CommonUtil;
import com.foucus.myfouces.Resource.Util.GsonImpl;
import com.foucus.myfouces.Resource.Util.JsonUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yf-06 on 2017/6/27.
 * <p>
 * 主頁
 */

public class MainActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.fly_main)
    FrameLayout flyMain;
    @Bind(R.id.img_homepage)
    ImageView imgHomepage;
    @Bind(R.id.tv_homepage)
    TextView tv_homepage;
    @Bind(R.id.lly_homepage)
    LinearLayout lly_Homepage;
    @Bind(R.id.img_attention)
    ImageView img_attention;
    @Bind(R.id.tv_attention)
    TextView tv_attention;
    @Bind(R.id.lly_attention)
    LinearLayout lly_attention;
    @Bind(R.id.img_photograph)
    ImageView img_photograph;
    @Bind(R.id.img_skill)
    ImageView img_skill;
    @Bind(R.id.tv_skill)
    TextView tv_skill;
    @Bind(R.id.lly_skill)
    LinearLayout lly_skill;
    @Bind(R.id.img_my)
    ImageView img_my;
    @Bind(R.id.tv_my)
    TextView tv_my;
    @Bind(R.id.lly_my)
    LinearLayout lly_my;
    private Activity mActivity;
    private FragmentManager fragmentManager;


    private HomePageFg homePageFg;//首页
    private FollowFg attentionFg;//关注
    private SkillFg skillFg;//技巧
    private MyFg myFg;//我的


    private boolean is_click_home = true, is_click_attention = false, is_click_skill = false, is_click_my = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mActivity = this;
        init();
    }

    private void init() {

        fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        homePageFg = new HomePageFg();
        attentionFg = new FollowFg();
        skillFg = new SkillFg();
        myFg = new MyFg();

        ft.add(R.id.fly_main, homePageFg, "one");
        ft.commitAllowingStateLoss();

        if (CommonUtil.isNetworkAvailable(mActivity)) {
            GetArgument();
        }
    }

    @OnClick({R.id.lly_homepage, R.id.lly_attention, R.id.lly_skill, R.id.lly_my, R.id.img_photograph})
    @Override
    public void onClick(View v) {
        FragmentTransaction ft;
        switch (v.getId()) {
            case R.id.lly_homepage://首页
                ft = fragmentManager.beginTransaction();
                if (lly_Homepage.isSelected()) {
                    return;
                }
                if (attentionFg.isAdded()) {
                    ft.hide(attentionFg);
                    attentionFg.onPause();
                }
                if (attentionFg.isAdded()) {
                    ft.hide(attentionFg);
                    attentionFg.onPause();
                }
                if (skillFg.isAdded()) {
                    ft.hide(skillFg);
                    skillFg.onPause();
                }
                if (myFg.isAdded()) {
                    ft.hide(myFg);
                    myFg.onPause();
                }
                ft.show(homePageFg);
                ft.commit();

                lly_Homepage.setSelected(true);
                lly_attention.setSelected(false);
                lly_skill.setSelected(false);
                lly_my.setSelected(false);

//                if (!sharedPreferencesUtil.getHomepageclick().equals("")) {
//                    com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getHomepageclick(),
//                            img_homepage, home_click_displayImageOptions);
//                }
//
//                if (is_click_attention) {
//                    if (!sharedPreferencesUtil.getAttention().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getAttention(),
//                                img_attention, attention_displayImageOptions);
//                    }
//                }
//
//                if (is_click_skill) {
//                    if (!sharedPreferencesUtil.getSkill().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getSkill(),
//                                img_skill, skill_displayImageOptions);
//                    }
//                }
//
//                if (is_click_my) {
//                    if (!sharedPreferencesUtil.getMy().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getMy(),
//                                img_my, my_displayImageOptions);
//                    }
//                }

                is_click_home = true;
                is_click_attention = false;
                is_click_skill = false;
                is_click_my = false;

                tv_homepage.setSelected(true);
                tv_attention.setSelected(false);
                tv_skill.setSelected(false);
                tv_my.setSelected(false);
                break;

            case R.id.lly_attention://关注
                if (lly_attention.isSelected()) {
                    return;
                }
                ft = fragmentManager.beginTransaction();
                if (homePageFg.isAdded()) {
                    ft.hide(homePageFg);
                    homePageFg.onPause();
                }
                if (attentionFg.isAdded()) {
                    ft.hide(attentionFg);
                    attentionFg.onPause();
                }
                if (skillFg.isAdded()) {
                    ft.hide(skillFg);
                    skillFg.onPause();
                }
                if (myFg.isAdded()) {
                    ft.hide(myFg);
                    myFg.onPause();
                }
                if (attentionFg.isAdded()) {
                    ft.show(attentionFg);
                } else {
                    ft.add(R.id.fly_main, attentionFg, "two");
                    ft.show(attentionFg);
                }
                ft.commit();

                lly_Homepage.setSelected(false);
                lly_attention.setSelected(true);
                lly_skill.setSelected(false);
                lly_my.setSelected(false);

//                if (is_click_home) {
//                    if (!sharedPreferencesUtil.getHomepage().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getHomepage(),
//                                img_homepage, home_displayImageOptions);
//                    }
//                }
//
//                if (!sharedPreferencesUtil.getAttentiomclick().equals("")) {
//                    com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getAttentiomclick(),
//                            img_attention, attention_click_displayImageOptions);
//                }
//
//                if (is_click_skill) {
//                    if (!sharedPreferencesUtil.getSkill().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getSkill(),
//                                img_skill, skill_displayImageOptions);
//                    }
//                }
//
//                if (is_click_my) {
//                    if (!sharedPreferencesUtil.getMy().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getMy(),
//                                img_my, my_displayImageOptions);
//                    }
//                }

                is_click_home = false;
                is_click_attention = true;
                is_click_skill = false;
                is_click_my = false;

                tv_homepage.setSelected(false);
                tv_attention.setSelected(true);
                tv_skill.setSelected(false);
                tv_my.setSelected(false);

                break;
            case R.id.lly_skill://技巧
                if (lly_skill.isSelected()) {
                    return;
                }
                ft = fragmentManager.beginTransaction();

                if (homePageFg.isAdded()) {
                    ft.hide(homePageFg);
                    homePageFg.onPause();
                }
                if (attentionFg.isAdded()) {
                    ft.hide(attentionFg);
                    attentionFg.onPause();
                }
                if (skillFg.isAdded()) {
                    ft.hide(skillFg);
                    skillFg.onPause();
                }
                if (myFg.isAdded()) {
                    ft.hide(myFg);
                    myFg.onPause();
                }
                if (skillFg.isAdded()) {
                    ft.show(skillFg);
                } else {
                    ft.add(R.id.fly_main, skillFg, "three");
                    ft.show(skillFg);
                }
                ft.commit();

                lly_Homepage.setSelected(false);
                lly_attention.setSelected(false);
                lly_skill.setSelected(true);
                lly_my.setSelected(false);

//                if (is_click_home) {
//                    if (!sharedPreferencesUtil.getHomepage().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getHomepage(),
//                                img_homepage, home_displayImageOptions);
//                    }
//                }
//
//                if (is_click_attention) {
//                    if (!sharedPreferencesUtil.getAttention().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getAttention(),
//                                img_attention, attention_displayImageOptions);
//                    }
//                }
//                if (!sharedPreferencesUtil.getSkillclick().equals("")) {
//                    com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getSkillclick(),
//                            img_skill, skill_click_displayImageOptions);
//                }
//
//                if (is_click_my) {
//                    if (!sharedPreferencesUtil.getMy().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getMy(),
//                                img_my, my_displayImageOptions);
//                    }
//                }

                is_click_home = false;
                is_click_attention = false;
                is_click_skill = true;
                is_click_my = false;

                tv_homepage.setSelected(false);
                tv_attention.setSelected(false);
                tv_skill.setSelected(true);
                tv_my.setSelected(false);

                break;
            case R.id.lly_my://个人
                if (lly_my.isSelected()) {
                    return;
                }
                ft = fragmentManager.beginTransaction();
                if (homePageFg.isAdded()) {
                    ft.hide(homePageFg);
                    homePageFg.onPause();
                }
                if (attentionFg.isAdded()) {
                    ft.hide(attentionFg);
                    attentionFg.onPause();
                }
                if (attentionFg.isAdded()) {
                    ft.hide(attentionFg);
                    attentionFg.onPause();
                }
                if (skillFg.isAdded()) {
                    ft.hide(skillFg);
                    skillFg.onPause();
                }
                if (myFg.isAdded()) {
                    ft.show(myFg);
                } else {
                    ft.add(R.id.fly_main, myFg, "four");
                    ft.show(myFg);
                }
                ft.commit();

                lly_Homepage.setSelected(false);
                lly_attention.setSelected(false);
                lly_skill.setSelected(false);
                lly_my.setSelected(true);
//                if (is_click_home) {
//                    if (!sharedPreferencesUtil.getHomepage().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getHomepage(),
//                                img_homepage, home_displayImageOptions);
//                    }
//                }
//
//                if (is_click_attention) {
//                    if (!sharedPreferencesUtil.getAttention().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getAttention(),
//                                img_attention, attention_displayImageOptions);
//                    }
//                }
//                if (is_click_skill) {
//                    if (!sharedPreferencesUtil.getSkill().equals("")) {
//                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getSkill(),
//                                img_skill, skill_displayImageOptions);
//                    }
//                }
//
//                if (!sharedPreferencesUtil.getMyclick().equals("")) {
//                    com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(sharedPreferencesUtil.getMyclick(),
//                            img_my, my_click_displayImageOptions);
//                }

                is_click_home = false;
                is_click_attention = false;
                is_click_skill = false;
                is_click_my = true;

                tv_homepage.setSelected(false);
                tv_attention.setSelected(false);
                tv_skill.setSelected(false);
                tv_my.setSelected(true);

                break;
            case R.id.img_photograph://拍摄

//                Intent intent = new Intent(getApplicationContext(), Text.class);
//                intent.putExtra("resources", "48485");//影片编号
//                startActivity(intent);

                //动态权限获取
//                requestPermission(1, Manifest.permission.WRITE_EXTERNAL_STORAGE, new Runnable() {
//                    @Override
//                    public void run() {
//                        WRITE_EXTERNAL_STORAGE();//允许
//                    }
//                }, new Runnable() {
//                    @Override
//                    public void run() {
//                        WRITE_EXTERNAL_STORAGE_Denied();//禁止
//                    }
//                });
                break;
        }
    }


    /**
     * 获取一些网站（意见反馈，关于我们）
     */

    private void GetArgument() {
        BaseReq req = new BaseReq();
        req.setValue("type", "arguments");
        ServiceFactory.getInstance()
                .createService(GankService.class)
                .GetArgument(req.getSecret("GetArgument"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("ssss", "22222==>" + e.toString());
                    }

                    @Override
                    public void onNext(ResponseBody argumentResp) {
                        String response = JsonUtil.JsonToStr(argumentResp);
                        boolean isflag = Setting.isRight(mActivity, response);
                        if (isflag) {
                            ArgumentResp resp = GsonImpl.get().toObject(response, ArgumentResp.class);
                            Log.e("ssss", "ArgumentResp==>" + resp.getResult().getAbout_us());
                        }
                    }


                });

    }


    /**
     * 按两次返回键退出程序
     */
    private long mExitTime;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出",
                        Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
